/*
 * This servlet controls the manage my lesson
 *
 */
package controllers.managecontrollers;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Course;
import models.Lesson;
import dao.CourseDao;
import classes.HibernateUtil;
import dao.LessonDao;

public class ManageMyLessonController extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HibernateUtil hibernate = new HibernateUtil();
        CourseDao cdao = new CourseDao(hibernate.getSessionFactory().openSession());
        List<Course> courseList = cdao.findAll();
        hibernate.close();
        request.setAttribute("courseList", courseList);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HibernateUtil hu = new HibernateUtil();
        long id = System.nanoTime();
        String code = request.getParameter("code");
        String name = request.getParameter("name");
        String description = request.getParameter("description");
        long courseid = Long.parseLong(request.getParameter("courseid"));
        CourseDao cdao = new CourseDao(hu.openSession());
        Course course = cdao.get(courseid);
        //checks the formoption if its Lessonadd
        if (request.getParameter("formoption").equals("Lessonadd")) {
            LessonDao ld = new LessonDao(hu.openSession());
            Lesson lec = new Lesson();

            //save Lesson object
            lec.setId(id);
            lec.setName(name);
            lec.setDescription(description);
            lec.setCourse(course);

            ld.save(lec);
            hu.close();

            response.sendRedirect("CourseLessonEdit");

        } else {//get the lesson by id
            id = Long.parseLong(request.getParameter("Lessonid"));

            LessonDao ld = new LessonDao(hu.openSession());

            Lesson lec = ld.get(id);

            lec.setName(name);
            lec.setDescription(description);
            lec.setCourse(course);
            ld.update(lec);
            hu.close();

            response.sendRedirect("CourseLessonEdit");
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
